package{
	import flash.external.ExtensionContext;
	import flash.utils.Dictionary;
	import flash.system.Capabilities;
	
	public class BangoInterface{
		private var context:ExtensionContext;
		
		private var infos:Object = null;
		
		public function BangoInterface(){
			try{
				if(!context){
					context = ExtensionContext.createExtensionContext("com.bango.infoext",null);
				}
			}catch(ex:Error){
				//trace("Context Error!" + ex.message + ex.getStackTrace());
			}
		}
		
		public function getInfos():void{
			try{
				var res:String = context.call("getInfos",null) as String;
				if(res != null)
					infos = JSON.parse(res);
			}catch(ex:Error){
				//trace("Library Error!" + ex.message + ex.getStackTrace());
			}
		}
		
		public function getProperty(key:String):String{
			getInfos();
			if(infos != null && infos[key] != null){
				return infos[key];
			}else{
				return getAIRProperty(key);
			}
			return "";
		}
		
		//We can't get something out of the Native extions, let's try to use AIR
		public function getAIRProperty(key:String):String{
			switch(key){
				case "OperatingSystem":
					return getAIROS();
				break;
				case "OperatingSystemVersion":
					return getAIROSVersion();
				break;
			}
			return "";
		}
		
		public function getAIROS():String{
			if((Capabilities.os.indexOf("Windows") >= 0)){
				return "Windows";
			}else if((Capabilities.os.indexOf("Mac") >= 0)){
				return "Mac";
			}else if((Capabilities.os.indexOf("Linux") >= 0)){
				return "Linux";
			}
			return Capabilities.os;
		}
		
		public function getAIROSVersion():String{
			return Capabilities.os.replace("Windows ","").replace("Mac OS ","");
		}
		
		public function listPropieties():String{
			
			getInfos();
			if(infos == null)
				return null;
			
			var res:String="";
			
			for(var i:String in infos)
				res = res + i +" : " + infos[i] + "\n";
			
			res = res.substring(0,res.length-2);
			return res;
		}
	}
}